home *** CD-ROM | disk | FTP | other *** search
/ Amiga Packmags / NewsFlash - Issue 07 (1990-01)(UGA - 17-Bit Software)(Disk 1 of 2)[a intro].zip / NewsFlash - Issue 07 (1990-01)(UGA - 17-Bit Software)(Disk 1 of 2)[a intro].adf / prog / DevPatchII.doc < prev    next >
Text File  |  1996-12-24  |  16KB  |  344 lines

  1.  
  2.  
  3.                                 'DevPatch II'
  4.                                 -------------
  5.                                by PowerPeak '89
  6.  
  7.     This is the first of a monthly series of little programs, tips, etc. we
  8. will try to provide for NewsFlash. (I hope we manage it)
  9.     These programs will be written in C and assembler.  For the assembler we
  10. use DevPac II.  I don't think the assembler programs will assemble with Seka
  11. without change and frankly I don't give a damn because Seka is shit, it's
  12. about time people start using a real assembler.  All these programs are
  13. clean, this means they will allocate/open and free/close all the things they
  14. need, they will use the operating system (why do you think there is one !)
  15. and they will NOT use absolute addresses !  Sorry to have to put it this
  16. way, but there is just far too much bad programming going on on the Amiga and
  17. it's high time people change their lousy programming habits.  The Amiga is a
  18. very professional multi-tasking computer and it deserves good programs !
  19.  
  20.     OK, so here we go:
  21.  
  22.     This first little utility was written by three people, the skeleton was
  23. written by Jorrit Tyberghein and the rest by all three of us.  The idea for
  24. the program came when we were assembling with DevPac II and we were low on
  25. chip memory.  As you may know DevPac opens a full screen window to print
  26. error messages while assembling.  It also opens a window for default output
  27. when you run your program.  These windows that are rarely used eat up 40K of
  28. precious chip memory !!!  We decided to do something about it and wrote this
  29. little utility : DevPatch II.
  30.  
  31.     The program will install a patch for OpenWindow, this means it will
  32. catch every OpenWindow call by ANY program so it can check the NewWindow
  33. structure passed to the function.  If a program tries to open a window with
  34. a title of 'Assembling...' or 'Default Output Window' the height will be
  35. forced to 45 pixels.  This way the DevPac windows will open, but they will
  36. be very small so they don't use up a lot of chip memory.  Problem solved !!
  37. The patch only requires 144 bytes of memory, so this shouldn't be a problem.
  38.     If you run the program a second time it will remove the patch.
  39.  
  40.     We find this program very useful, we hope you do as well.
  41.  
  42.                                                                              
  43.                                 DevPatch II                                  
  44. -----------------------------------------------------------------------------
  45.                                                                              
  46.           OPT c+,l-
  47.  
  48. ****************************************
  49. * DevPatch II                          *
  50. * © J.Tyberghein N.François P.Marivoet *
  51. * Dec 1989                             *
  52. ****************************************
  53.  
  54. -----------------------------------------------------------------------------
  55.    You will probably have to change this include directory.                  
  56. -----------------------------------------------------------------------------
  57.                                                                              
  58.    incdir  "DevPacII:include"
  59.    include "exec/types.i"
  60.    include "exec/memory.i"
  61.    include "exec/ports.i"
  62.    include "exec/exec_lib.i"
  63.  
  64. -----------------------------------------------------------------------------
  65.    Some equals so we don't have to include too much.                         
  66. -----------------------------------------------------------------------------
  67.                                                                              
  68.    ;IntuitionBase routines
  69. _LVOOpenWindow  equ   -204
  70.    ;DosBase
  71. _LVOOutput      equ   -60
  72. _LVOWrite       equ   -48
  73.  
  74. nw_Title        equ   $1a
  75. nw_Height       equ   6
  76. PRI             equ   0
  77.  
  78.    ;*** Start code ***
  79.  
  80. -----------------------------------------------------------------------------
  81.    Open all the necessary libraries.
  82. -----------------------------------------------------------------------------
  83.                                                                              
  84.    ;IntuitionLibrary
  85.       lea IntLib(PC),a1
  86.       CALLEXEC   OldOpenLibrary
  87.       move.l d0,_IntBase
  88.    ;DosLibrary
  89.       lea DosLib(PC),a1
  90.       CALLEXEC   OldOpenLibrary
  91.       move.l d0,_DosBase
  92.  
  93. -----------------------------------------------------------------------------
  94.    Print copyright message to screen.                                        
  95. -----------------------------------------------------------------------------
  96.                                                                              
  97.    ;Say we exist
  98.       lea StartupMsg(PC),a2
  99.       moveq #StartupMsgLen,d2
  100.       bsr myPuts
  101.  
  102. -----------------------------------------------------------------------------
  103.    Here we test to see if the patch is already installed.  We do this by     
  104.    looking for the named message port we left open when we installed the     
  105.    patch.                                                                    
  106. -----------------------------------------------------------------------------
  107.                                                                              
  108.    ;Test if the MsgPort exists
  109.       lea StartBlock(PC),a1
  110.       lea PortName(a1),a1
  111.       CALLEXEC FindPort
  112.       tst.l d0
  113.       bne.s PortAlreadyExists
  114.  
  115. -----------------------------------------------------------------------------
  116.    The message port didn't exist so we must install the patch.               
  117.    First we allocate memory for the message port structure AND the patch     
  118.    code.                                                                     
  119. -----------------------------------------------------------------------------
  120.                                                                              
  121.    ;Create MsgPort
  122.       move.l #BlockLen,d0
  123.       move.l #MEMF_CLEAR+MEMF_PUBLIC,d1
  124.       CALLEXEC AllocMem
  125.       tst.l d0
  126.       beq CloseLib
  127.  
  128. -----------------------------------------------------------------------------
  129.    Now copy the message port and the code to the allocated block.            
  130. -----------------------------------------------------------------------------
  131.                                                                              
  132.       move.l d0,Port
  133.       lea StartBlock(PC),a0
  134.       move.l d0,a1
  135.       move.l #BlockLen,d0
  136.       CALLEXEC CopyMem
  137.  
  138. -----------------------------------------------------------------------------
  139.    Ask EXEC to add our port.  The message port is not correctly initialized, 
  140.    but since this is a dummy port this is not important.  The port is only   
  141.    used to find our patch the second time we get run.                        
  142. -----------------------------------------------------------------------------
  143.                                                                              
  144.       move.l Port(PC),a1
  145.       lea PortName(a1),a0
  146.       move.l a0,LN_NAME(a1)
  147.       CALLEXEC AddPort
  148.  
  149. -----------------------------------------------------------------------------
  150.    Now, install the OpenWindow patch !                                       
  151.    After the CALLEXEC D0 holds the old address of OpenWindow, we put this    
  152.    into our jmp instruction at the end of the patch.                         
  153. -----------------------------------------------------------------------------
  154.                                                                              
  155.    ;Install patch
  156.       move.l Port(PC),a1
  157.       lea OpenPatch(a1),a0
  158.       move.l a0,d0
  159.       move.l _IntBase(PC),a1
  160.       move.l #_LVOOpenWindow,a0
  161.       CALLEXEC   SetFunction
  162.       move.l Port(PC),a1
  163.       move.l d0,JmpLab1(a1)
  164.  
  165. -----------------------------------------------------------------------------
  166.    From this point on our OpenWindow is called.                              
  167.    Inform the user we installed the patch and exit.                          
  168. -----------------------------------------------------------------------------
  169.                                                                              
  170.       lea PatchedMsg(PC),a2
  171.       moveq #PatchedMsgLen,d2
  172.       bsr.s myPuts
  173.       bra.s CloseLib
  174.  
  175. -----------------------------------------------------------------------------
  176.    Here, the port already exists and we have to remove the patch (and port). 
  177. -----------------------------------------------------------------------------
  178.                                                                              
  179. PortAlreadyExists:
  180.       move.l d0,a2
  181.  
  182. -----------------------------------------------------------------------------
  183.    First restore the old OpenWindow function.                                
  184. -----------------------------------------------------------------------------
  185.                                                                              
  186.    ;Remove patch
  187.       move.l JmpLab1(a2),d0
  188.       move.l _IntBase(PC),a1
  189.       move.l #_LVOOpenWindow,a0
  190.       CALLEXEC   SetFunction
  191.  
  192. -----------------------------------------------------------------------------
  193.    Remove the message port from the system list.                             
  194. -----------------------------------------------------------------------------
  195.                                                                              
  196.    ;Remove Port
  197.       move.l a2,a1
  198.       CALLEXEC RemPort
  199.  
  200. -----------------------------------------------------------------------------
  201.    Free the memory we allocated for the message port and the code.           
  202. -----------------------------------------------------------------------------
  203.                                                                              
  204.    ;Free Patch memory
  205.       move.l a2,a1
  206.       move.l #BlockLen,d0
  207.       CALLEXEC FreeMem
  208.  
  209. -----------------------------------------------------------------------------
  210.    Tell user what happened.                                                  
  211. -----------------------------------------------------------------------------
  212.                                                                              
  213.    ;Say it's done
  214.       lea RemovedMsg(PC),a2
  215.       moveq #RemovedMsgLen,d2
  216.       bsr.s myPuts
  217.  
  218. -----------------------------------------------------------------------------
  219.    Close everything we opened before we exit !                               
  220. -----------------------------------------------------------------------------
  221.                                                                              
  222. CloseLib:
  223.    ;Close libraries
  224.       move.l _DosBase(PC),a1
  225.       CALLEXEC   CloseLibrary
  226.       move.l _IntBase(PC),a1
  227.       CALLEXEC   CloseLibrary
  228.       rts
  229.  
  230. -----------------------------------------------------------------------------
  231.    Print a message to the console.                                           
  232. -----------------------------------------------------------------------------
  233.                                                                              
  234. myPuts:
  235.       move.l _DosBase(PC),a6
  236.       jsr _LVOOutput(a6)
  237.       move.l d0,d1
  238.       move.l d2,d3
  239.       move.l a2,d2
  240.       jsr _LVOWrite(a6)
  241.       rts
  242.  
  243. -----------------------------------------------------------------------------
  244.    The part between stars is the block that gets copied when we install the  
  245.    patch.  It holds a message port structure and the actual patch code.      
  246.    This will need very little memory.                                        
  247.    The patch code MUST be PC-relative !!! (so e.g. NO 'move.l d0,label')     
  248. -----------------------------------------------------------------------------
  249.                                                                              
  250. *****************************************************************************
  251.  
  252.    ;This is our MsgPort followed by our patch function
  253.    ;this data and code will be copied in one block
  254. StartBlock:
  255.       dc.l   0,0
  256.       dc.b   NT_MSGPORT,PRI
  257.       dc.l   0                     ;Pointer to our MsgPort name
  258.       dc.b   0,0
  259.       dc.l   0,0,0,0
  260.       dc.b   0,0
  261.  
  262. -----------------------------------------------------------------------------
  263.    The patch !!!!  First save all registers we use !!! (VERY IMPORTANT)      
  264. -----------------------------------------------------------------------------
  265.                                                                              
  266. OpenPatch equ *-StartBlock
  267.       movem.l   d7/a2-a3,-(a7)
  268.  
  269. -----------------------------------------------------------------------------
  270.    A0 holds address of NewWindow structure.  Check if the title equals       
  271.    'Assembling...' or 'Default Output Window'.                               
  272. -----------------------------------------------------------------------------
  273.                                                                              
  274.       move.l nw_Title(a0),a2
  275.       lea Assembling(PC),a3
  276.       moveq #13,d7
  277. CmpLoop:
  278.       cmp.b (a2)+,(a3)+
  279.       dbne d7,CmpLoop
  280.       addq.w #1,d7
  281.       beq.s PatchIt
  282.       move.l nw_Title(a0),a2
  283.       lea DefaultOutput(PC),a3
  284.       moveq #21,d7
  285. CmpLoop2:
  286.       cmp.b (a2)+,(a3)+
  287.       dbne d7,CmpLoop2
  288.       addq.w #1,d7
  289.       bne.s NotDevpac
  290.  
  291. -----------------------------------------------------------------------------
  292.    The title was recognized, so we force the window height to 45             
  293. -----------------------------------------------------------------------------
  294.                                                                              
  295. PatchIt:
  296.       move.w #45,nw_Height(a0)
  297. NotDevpac:
  298.       movem.l (a7)+,d7/a2-a3
  299.  
  300. -----------------------------------------------------------------------------
  301.    Jump to old OpenWindow (most probably in KickStart)                       
  302. -----------------------------------------------------------------------------
  303.                                                                              
  304. JmpLab1 equ *+2-StartBlock
  305.       jmp $0.l
  306.  
  307. -----------------------------------------------------------------------------
  308.    Portname and strings to compare with.  Note that it is VERY important     
  309.    that these strings are in the allocated block, otherwise they become      
  310.    invalid when your program exits !!!                                       
  311. -----------------------------------------------------------------------------
  312.                                                                              
  313. PortName equ *-StartBlock
  314.                  dc.b "DevPatch.port",0
  315. Assembling:      dc.b "Assembling...",0
  316. DefaultOutput:   dc.b "Default Output Window",0
  317.  
  318. BlockLen equ *-StartBlock
  319.  
  320. *****************************************************************************
  321.  
  322. -----------------------------------------------------------------------------
  323.    Data                                                                      
  324. -----------------------------------------------------------------------------
  325.                                                                              
  326. _IntBase:     dc.l 0
  327. _DosBase:     dc.l 0
  328. Port:         dc.l 0
  329. IntLib:       dc.b "intuition.library",0
  330. DosLib:       dc.b "dos.library",0
  331. StartupMsg:   dc.b 27,"[33mDevPatch II",27,"[0m by PowerPeak",10,0
  332. StartupMsgLen equ *-StartupMsg
  333. PatchedMsg:   dc.b "Patch installed.",10,0
  334. PatchedMsgLen equ *-PatchedMsg
  335. RemovedMsg:   dc.b "Patch removed.",10,0
  336. RemovedMsgLen equ *-RemovedMsg
  337.  
  338.    END
  339.  
  340. -----------------------------------------------------------------------------
  341.    The end.                                                   Nico François  
  342. -----------------------------------------------------------------------------
  343.                                                                              
  344.